const db = wx.cloud.database();
Page({
  data:{
    moreLovegoods: [],
    goodsList: [],
    activeList: []
  },
  async onLoad(){

    let goodsList = await db.collection('goodsList')
    .orderBy('good_love','desc')
    .get() //获取数据库中根据字段good_love降序排列的数据
    
    //取购买排名前四的数据
    let moreLovegoods = []
    for(let i=0;i<4;i++){
      moreLovegoods.push(goodsList.data[i]) 
    }
    // console.log(goodsList.data)

    let leiNames = goodsList.data.map( item => item.good_belong)  //取商品所属分类名称数组
    let leiList = []

    for(let x=0;x<leiNames.length;x++){    //数据去重复
      if(!leiList.includes(leiNames[x])){
        leiList.push(leiNames[x])
      }
    }
    // console.log(leiList)
    let fenleiList = []
    for(let y=0;y<leiList.length;y++){  //数组中总共类名称数量
      fenleiList[y] = {}
      fenleiList[y].lei_name = leiList[y]
      fenleiList[y].children = []
      for(let x=0;x<goodsList.data.length;x++){
        if(fenleiList[y].lei_name == goodsList.data[x].good_belong){
          fenleiList[y].children.push(goodsList.data[x])
        }
      }
    }
    //  console.log(fenleiList)
    this.setData({
      goodsList: fenleiList,
      moreLovegoods,
      activeList: fenleiList[0].children
    })
  },

  // 点击导航
  changeIndex(ev){
    const index = parseInt(ev.currentTarget.id)
    // console.log(index)
    let activeList = this.data.goodsList[index].children
    this.setData({
      activeList
    })
  },

  //点击加入购物车
  toCartPage(ev){
    // console.log(ev.currentTarget.dataset.item.good_id)
    // let newCarList = getApp().globalData.carList
    // if(newCarList.length ==0){  //首次添加的元素
    //   newCarList.push(ev.currentTarget.dataset.item)
    // }else{
    //   for(let x=0;x<newCarList.length;x++){
    //     if(newCarList[x].good_id != ev.currentTarget.dataset.item.good_id){
    //       newCarList.push(ev.currentTarget.dataset.item)
    //       newCarList[x].num = 1
    //     }else{
    //       newCarList[x].num ++
    //     }
    //   }
    // }
    // console.log(newCarList)
    // getApp().globalData.carList.push(ev.currentTarget.dataset.item)
    // wx.switchTab({  //跳转到tabbar不能传参问题，可以把要传递数据定义在app全局数据中
    //   url: '/pages/car/car?',
    // }) 第一次实现于修改
    let carList = getApp().globalData.carList
    let element = ev.currentTarget.dataset.item
    if(carList.length ==0){  //首次添加的元素
      element.num = 1
      carList.push(element)
    }else{   //当数组有元素后
      let flag = carList.find(item => item.good_id == element.good_id)
      if(flag == undefined){  //数组中没有此对象
        element.num = 1
        carList.push(element) //把元素添加到数组中并且添加num 属性
      }else{
        flag.num ++
      }
    }
    // console.log(carList)
    let totalnum = 0
    for(let x=0;x<carList.length;x++){
      totalnum += carList[x].num
    }
    let length = totalnum.toString()
    wx.setTabBarBadge({
      index: 1,
      text: length
    })
  },

  goToPay(){
    if(getApp().globalData.userInfo){
      console.log('可以吊起支付功能')
    }else{
      wx.showToast({
        title: '授权等候后方可购买商品哟！！',
      })
      setTimeout( () => {
        wx.navigateTo({
          url: '/pages/log/log',
        })
      },2000)
    }
  }

})